<template>
    <div>
        <el-container style="height: 500px; border: 1px solid #eee">
            <el-header style="text-align: right; font-size: 12px">
                <div>
                    <span class="title">学生管理系统</span>
                    <img class="titleimg" src="/xuexiao.png">
                </div>
                <span style="color: white;font-size: 14px;">{{name}}</span>
                <span @click="edithome" style="margin-left: 40px;font-size: 14px;cursor: pointer">退出</span>
                <div class="admin">
                    <el-dropdown>
                        <span class="el-dropdown-link">
    <i style="color: white;" class="el-icon-arrow-down el-icon--right"></i>
  </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item @click="dialogFormVisible=true">添加管理员</el-dropdown-item>
                                <el-dropdown-item @click="dialogFormVisible1=true">修改密码</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
                <el-dialog title="添加管理员" v-model="dialogFormVisible">
                    <el-form :rules="rules" :model="form">
                        <el-form-item prop="name" label="账号" label-width="">
                            <el-input v-model="form.name" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item prop="pwd" label="密码" label-width="">
                            <el-input type="password" v-model="form.pwd" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item prop="checkpwd" label="确认密码" label-width="">
                            <el-input type="password" v-model="form.checkpwd" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addadmin">确 定</el-button>
    </span>
                    </template>
                </el-dialog>

                <el-dialog title="修改密码" v-model="dialogFormVisible1">
                    <el-form :rules="rules" :model="form">
                        <el-form-item prop="pwd" label="密码" label-width="">
                            <el-input type="password" v-model="form.pwd" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item prop="checkpwd" label="确认密码" label-width="">
                            <el-input type="password" v-model="form.checkpwd" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogFormVisible1 = false">取 消</el-button>
      <el-button type="primary" @click="updateadmin">确 定</el-button>
    </span>
                    </template>
                </el-dialog>



            </el-header>

            <el-container>

                <el-aside width="200px">
                    <el-menu unique-opened="true" text-color="#fff" background-color="#172838" :default-openeds="['1']"
                             :router="true" :default-active="activeIndex">
                        <el-submenu @click="after='学生管理'" index="1">
                            <template #title><i class="el-icon-user"></i>学生管理</template>
                            <el-menu-item-group>
                                <el-menu-item @click="before='学生列表'" index="/studentList">学生列表</el-menu-item>
                            </el-menu-item-group>
                        </el-submenu>

                        <el-submenu @click="after='课程管理'" index="2">
                            <template #title><i class="el-icon-notebook-2"></i>课程管理</template>
                            <el-menu-item-group>
                                <el-menu-item @click="before='课程信息'" index="/classlist">课程信息</el-menu-item>
                            </el-menu-item-group>

                        </el-submenu>

                    </el-menu>

                </el-aside>
                <el-main>
                    <div style="margin-bottom: 20px"><span class="spannav">{{after}}<span class="spanmid">/</span>{{before}}</span>
                    </div>
                    <router-view/>

                </el-main>
            </el-container>
        </el-container>
    </div>

</template>

<style>
    .titleimg {
        margin-left: 10px;
        height: 60px;
        width: 60px;
        float: left;
    }

    .title {
        color: #eeeeee;
        float: left;
        font-size: 18px;
    }

    .spanmid {
        margin: 0 9px;
        font-weight: 700;
        color: #C0C4CC;
    }

    .spannav {
        font-weight: 400;
        color: #606266;
        cursor: text;
    }

    .el-header {
        background-color: #0e1b24;
        color: #333333;
        line-height: 60px;
        align-items: center;
        color: #fff;
    }

    .el-menu {
        width: 100%;
    }

    .el-aside {
        background-color: #172838;
        height: 100vh;
    }

    .el-main {
        background-color: #e4e7ed;
        height: 100vh;
        padding: 20px;
    }

    .admin {
        cursor: pointer;
        position: absolute ;
        top:32px;
        right: 6.5%;

    }

    .admin span {

    }

    .el-dialog__title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
        float: left;
    }
</style>

<script>

    import {mapState} from "vuex";
    import {updateadmin, addadmin, selectadmin} from "../network/admin";

    export default {

        computed: {
            ...mapState(['name'])
        },
        mounted() {

        },
        created() {
            this.activeIndex = this.$route.path
            if (this.activeIndex == '/studentList') {
                this.after = "学生管理"
                this.before = '学生列表'
            }
            if (this.activeIndex == '/querystudent') {
                this.after = "学生管理"
                this.before = '学生详情'
            }
            if (this.activeIndex == '/classlist') {
                this.after = "课程管理"
                this.before = '课程信息'
            }
        },
        watch: {
            after() {
                this.before = ''
            }
        },
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkpwd !== '') {
                        this.$refs.ruleForm.validateField('checkpwd');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.pwd) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                dialogFormVisible1: false,
                after: '',
                before: '',
                activeIndex: '',
                dialogFormVisible: false,
                form: {
                    name: '',
                    pwd: '',
                    checkpwd: '',
                    id: ''
                },
                rules: {
                    name: [
                        {required: true, message: '账号不能为空'}
                    ],
                    pwd: [
                        {validator: validatePass, trigger: 'blur'},
                        {required: true, message: '密码不能为空'}
                    ],
                    checkpwd: [
                        {validator: validatePass2, trigger: 'blur'},
                        {required: true, message: '确认密码不能为空'}
                    ],
                }
            };
        }
        ,
        methods: {
            updateadmin() {
                selectadmin(this.name).then((res) => {
                    this.form.id = res.data.admins[0].id
                    if (this.form.pwd == this.form.checkpwd) {
                        this.dialogFormVisible1 = false
                        updateadmin(this.form.id,this.name, this.form.checkpwd).then((res1) => {
                            if (res1.data==true){
                                this.$message({
                                    type: 'success',
                                    message: "修改成功"
                                })
                            }
                        })
                    }
                })

            },
            edithome() {
                this.$store.commit('adminname', '')
                this.$router.push('/')
            },
            addadmin() {
                if (this.form.pwd == this.form.checkpwd) {
                    this.dialogFormVisible = false
                    addadmin(this.form.name, this.form.pwd).then((res) => {
                        if (res.data == true) {
                            this.$message({
                                type: 'success',
                                message: "添加成功"
                            });
                        }
                    })
                }
            }
        }
    };
</script>
